const momentService = require("../service/moment.service");

class MomentController {
  async create(ctx, next) {
    const { content } = ctx.request.body;
    const { id } = ctx.user;

    // 将动态相关数据保存到数据库
    const result = await momentService.create(content, id);
    ctx.body = {
      code: 0,
      msg: "创建用户动态成功",
      data: result,
    };
  }
  async list(ctx, next) {
    const { offset = 0, size = 10 } = ctx.request.body;
    // 查询到数据库
    const result = await momentService.queryList(offset, size);
    ctx.body = {
      code: 0,
      msg: "success",
      data: result,
    };
  }
  async detail(ctx, next) {
    /* 
    * 接口如果如 get 请求 ： /detail/:detailId
          const { detailId } = ctx.params;
    * method get /detail
          const { detailId } = ctx.qurey;
    */
    const { id } = ctx.request.body;
    // 查询到数据库

    const result = await momentService.queryById(id);
    console.log(id);
    ctx.body = {
      code: 0,
      msg: "success",
      data: result,
    };
  }
  async update(ctx, next) {
    /* 
    * 接口如果如 get 请求 ： /detail/:detailId
          const { detailId } = ctx.params;
    * method get /detail
          const { detailId } = ctx.qurey;
    */
    const { momentId, content } = ctx.request.body;
    // 查询到数据库

    const result = await momentService.update(content, momentId);

    ctx.body = {
      code: 0,
      msg: "修改动态成功",
      data: result,
    };
  }
  async delete(ctx, next) {
    /* 
    * 接口如果如 get 请求 ： /detail/:detailId
          const { detailId } = ctx.params;
    * method get /detail
          const { detailId } = ctx.qurey;
    */
    const { momentId } = ctx.request.body;
    // 查询到数据库

    const result = await momentService.deleteById(momentId);
    ctx.body = {
      code: 0,
      msg: "删除成功",
      data: result,
    };
  }
}
module.exports = new MomentController();
